using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;

namespace OA.BLL
{
    public class IPBlackWhite:Base
    {
        private DAL.IPBlackWhite dal = null;

        public IPBlackWhite()
        {
            isession = DB_help.NHibernateHelper.GetCurrentSession(DefaultDataBase);
            dal = new DAL.IPBlackWhite(isession);
        }

        public Common.Result Add(Model.IPBlackWhite value)
        {
            Common.Result ret = new Common.Result(false, 0, "添加失败");
            ret = dal.Add(value);
            return ret;
        }

        public Common.Result Update(Model.IPBlackWhite value)
        {
            Common.Result ret = new Common.Result(false, 0, "修改失败");
            var dalvalue = dal.GetModel(value.ID);
            if (dalvalue == null)
            {
                ret.success = false;
                ret.count = 0;
                ret.message = "数据不存在";
                return ret;
            }
            dalvalue.Type = value.Type;
            dalvalue.BeginIP = value.BeginIP;
            dalvalue.EndIP = value.EndIP;
            dalvalue.Memo = value.Memo;
            dalvalue.Status = value.Status;
            ret = dal.Update(dalvalue);
            return ret;
        }

        public Common.Result Delete(string ids)
        {
            var ret=dal.Delete(ids);
            return ret;
        }

        public Model.IPBlackWhite Get(int id)
        {
            return dal.GetModel(id);
        }

        public IList<Model.IPBlackWhite> Get(string where, string orderby, int start, int length, params object[] ps)
        {
            return dal.Get(where, orderby, start, length,ps);
        }

        public int GetCount(string where, params object[] ps)
        {
            return dal.GetCount(where, ps);
        }

        public bool IsAllowVisit(string ip)
        {
            bool ret = false;
            var list=dal.Get("? between BeginIP and EndIP and Status=1", null, ip);
            ret = list.Any(r => r.Type == 2) || !list.Any(r => r.Type == 1);
            return ret;
        }

    }
}
